package com.project.internship.controller.base;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.internship.entity.Bases;
import com.project.internship.entity.resp.RespBean;
import com.project.internship.entity.resp.RespPageBean;
import com.project.internship.service.BasesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 学院端基地信息
 */
@RestController
@RequestMapping("/base/info")
public class BaseInfoController {

    @Autowired
    BasesService basesService;

    /**
     * 获取基地信息
     * @param current
     * @param size
     * @param keyword
     * @return
     */
    @GetMapping("/")
    public RespPageBean getAllOwnBases(@RequestParam(defaultValue = "1") Integer current,
                                       @RequestParam(defaultValue = "10") Integer size,
                                       String keyword){
        Page<Bases> page = new Page<>(current,size);

        Long total = basesService.getAllOwnBases(page,keyword).getTotal();
        List<Bases> data =  basesService.getAllOwnBases(page,keyword).getRecords();

        return new RespPageBean(total,data);
    }

    /**
     * 添加基地
     * @param base
     * @return
     */
    @PostMapping("/")
    public RespBean addBase(@RequestBody Bases base){
        if(basesService.addBase(base) == 1){
            return RespBean.ok("添加成功！");
        }
        return RespBean.error("添加失败！");
    }

    /**
     * 更新基地信息
     * @param base
     * @return
     */
    @PutMapping("/")
    public RespBean updateBase(@RequestBody Bases base){
        if(basesService.updateBase(base) == 1){
            return RespBean.ok("更新成功！");
        }
        return RespBean.error("更新失败！");
    }

    /**
     * 删除一条基地信息
     * @param id 基地表中的主键id
     * @return
     */
    @DeleteMapping("/{id}")
    public RespBean deleteBase(@PathVariable("id") Long id){
        if(basesService.deleteBase(id) == 1){
            return RespBean.ok("删除成功！");
        }
        return RespBean.error("删除失败！");
    }

    /**
     * 批量删除基地信息
     * @param ids
     * @return
     */
    @DeleteMapping("/")
    public RespBean deleteBases(@RequestBody Long[] ids){
        if(basesService.deleteBases(ids) == ids.length){
            return RespBean.ok("删除成功！");
        }
        return RespBean.error("删除失败！");
    }

}
